﻿using Cat.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;

namespace PicServer.Yande
{
    class Yande_day_download
    {
        int page_count = 1;
        int try_count = 0;
        Queue<string> download_list = new Queue<string>();

        public void yande_list_scanner()
        {
            if (page_count < YandeStaticValue.MaxSacnnerPage)
            {
                String page_url = string.Format(YandeStaticValue.yandePicList_Link, page_count++);
                new Web_helper().HttpGetAsync(page_url, List_complete);
                Console.WriteLine("当前扫描列表页面：{0},链接:{1}", page_count, page_url);
            }
            else
            {
                Console.WriteLine("列表扫描结束,总计待处理：" + download_list.Count);
                //   content_Scanner();
                down_satart();


            }
        }

        private void down_satart()
        {
            string download_path = YandeStaticValue.yandeDay_path + DateTime.Now.ToLongDateString().ToString() + "\\";//年月日性质组织
            if (download_list.Count > 0)
            {
                string img_url = download_list.Dequeue();
                bool flag = new Html_helper().DownLoad_helper(download_path, img_url);
                if (flag)
                {

                    string img_id = Regex.Match(img_url, "/yande.re%20[0-9]{1,15}%20").ToString(); ;
                    img_id = Regex.Match(img_id, "%20[0-9]{1,15}%20").ToString().Replace("%20", "");
                    Console.WriteLine("当前页面ID：" + img_id);
                    YandeStaticValue.yandeOldUrl_list.Add(img_id);
                    new File_helper().Log_Update(YandeStaticValue.yandeDay_path + "old_id.txt", img_id);
                }
                else
                {

                    if (try_count < 3)
                    {
                        flag = new Html_helper().DownLoad_helper(download_path, img_url);

                        Console.WriteLine("第{0}次尝试", try_count++);
                        return;
                    }
                    else
                    {
                        Console.WriteLine("放弃重试！");
                    }
                }
                down_satart();
            }
            else
            {
                Console.WriteLine("全部下载完成");
            }

        }

        private void List_complete(object sender, OpenReadCompletedEventArgs e)
        {
            string shtml = Html_helper.get_html(e);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(shtml);

            XmlNodeList Post_single = doc.SelectNodes("/posts/post");

            Console.WriteLine(Post_single[0].Attributes["id"].Value);//获取该节点的某个值

            foreach (XmlNode singleNode in Post_single)
            {
                //  Console.WriteLine(singleNode.Attributes["jpeg_url"].Value);
                string id = singleNode.Attributes["id"].Value;
                string url = singleNode.Attributes["jpeg_url"].Value;
                if (!download_list.Contains(url) && !YandeStaticValue.yandeOldUrl_list.Contains(id))
                    download_list.Enqueue(url);
            }

            yande_list_scanner();
        }
    }
}
